/*=================  This runs App Fig 3: Event study using dube cont (spec=5) and BMS cont (spec=4) ====================*/
import delimited "${data2}mw_2020.csv",clear
tempfile 2020
save `2020'

use  "${data2}mw_state_annual.dta",clear
rename statefips state_fips
rename  mean_mw mw
merge m:1 state_fips using "${data2}state_geocodes.dta", nogen keep(3)

tempfile mw
save `mw'

keep if year ==2019
replace year = 2020
merge 1:1 state_name using `2020', nogen keep(3)
append using `mw'

replace year = year +1
keep if year >=1984 & year<=2013
 keep state_fips year mw
tab year
xtset state_fips year

gen F5mw = F5.mw
gen F4mw = F4.mw
gen F3mw = F3.mw
gen F2mw = F2.mw
gen F1mw = F1.mw
gen L1mw = L1.mw
gen L2mw = L2.mw
gen L3mw = L3.mw
gen L4mw = L4.mw
gen L5mw = L5.mw
order state_fips year *F* mw *L*



gen mw_d1=d1.mw
cap drop D1

local v mw_d1


g lnMWF3 = ln(F3mw)
g lnMWF2 = ln(F2mw)
g lnMWF1 = ln(F1mw)
g lnMW   = ln(mw)
g lnMWL1 = ln(L1mw)
g lnMWL2 = ln(L2mw)
g lnMWL3 = ln(L3mw)

local first 3
local last 3
forval f = `first'(-1)1 {
sort state_fips  year
gen EF`f' = F`f'.`v'
label var  EF`f' "-`f'"
replace EF`f' = 0 if EF`f'==.
if `f' ==`first' label var  EF`f' "-`f'+"
}

forval l = 0/`last'  {
gen EL`l' = L`l'.`v'
label var  EL`l' "`l'"
replace EL`l' = 0 if EL`l'==.
if `l' ==`last' label var  EL`l' "`l'+"
}

order EF* EL* , last
gsort state_fips  -year
by state_fips : replace EF`first' = sum(EF`first')
gsort state_fips  year
by state_fips : replace EL`last' = sum(EL`last')
tempfile mw 
save `mw'



local stat summstat(N)
local append outreg, merge(r) starloc(1) var stats(b se) blankrow  merge starlevels(10 5 1) bdec(3) `stat'  keep(tobacco21_treated ) nolegend



local  dem i.race##i.post2002 i.marst i.famsize i.nchild i.educ i.hispan##i.post2002 i.sex i.nchild##i.pre1988  i.race#i.pre1988
local  dem1 i.dem_race##i.post2002  i.dem_race#i.pre1988 i.dem_married i.dem_famsize ///
i.dem_nchild i.dem_nchild##i.pre1988 i.dem_educ i.dem_female i.dem_hisp##i.post2002


local  a1 year i.state_fips `dem'
local  a2 year i.state_fips `dem' strecdums2 c.year##i.state_fips i.year#i.division
local  a3 year i.state_fips `dem'

local rhs1 age* unemprate statepcgdp stateeitcrate //Dube Cont
local rhs2 age* unemprate statepcgdp stateeitcrate // Dube Cont
local rhs3 age* ur_high_edu wage_high_edu hpi eitc // Our Cont


local j 1
local d 1


use  "${data1}march_regready_1984.dta", clear
replace contpov = contpov2
replace eitc = eitc*eitc_ref

keep if year<=2013

merge m:1 state_fips year using `mw'

	
local q = `j' * 100
di "Poverty threshold `q'"
replace EF1=0

foreach spec of numlist 1/3 {
  cap drop under
  if `spec'==3 g  under = contpov < `j'
  else g under = contpov2 < `j'
  local q = `j' * 100
  reghdfe under EF* EL* `rhs`spec''  [pw=wtsupp] if demgroup`d'==1, a(`a`spec'') cluster(state_fips)
  est save "${est}ster/es_`q'_dem`d'_s`spec'_8413", replace
}





*plot
local j 2
local spec 4
local thresholdlist  1
    forvalues d = 1/1 {
  forvalues spec = 1/3 {
  foreach j of numlist `thresholdlist' {
  cap drop under
  if `spec'==3 g  under = contpov < `j'
  else g under = contpov2 < `j'
  local q = `j' * 100
  di "Poverty threshold `q'"
  if "`d'"=="1" local lab "Non-Elderly, < 65"
  if "`d'"=="2" local lab "All Ages, Including Elderly"
  if "`d'"=="3" local lab "Working Age Adults, Ages 16-64"
  if "`d'"=="4" local lab "≤ HS Degree, Ages 16-64"
  if "`d'"=="5" local lab "< HS Degree, Ages 16-24"
  if "`d'"=="6" local lab "Single Mothers, Ages 16-49"
  if "`d'"=="7" local lab "Black or Hispanic, Ages 16-64"
  if "`d'"=="8" local lab "Children Under Age 16"

  local range yla(-0.05(0.025)0.05)

  estimates use ${est}ster/es_`q'_dem`d'_s`spec'_8413
  est store D
  		coefplot ///
  			(D, omitted keep(EF* EL*)  msize(small) recast(connected) lcolor(gs1) color(gs1) lwidth(thin) ciopts(recast(rcap)  lcolor(gs10) lwidth(thin))),  ///
  			vertical xline(3.5) yline(0) graphregion(color(white)) yla(, nogrid) ///
  		   `range' ///
         legend(off) nooffsets ///
  			///title("`lab', ITN<`j'" , color(black))  /// "spec `spec'"
  			ytitle("Estimated Effect of Change in Minimum Wage") xtitle("Years Since Minimum Wage Increases")
  		graph export "${fig}event_studies/paper/es_`q'_dem`d'_s`spec'_8413.png", replace

  }
}
}
